{% block content %}
{% load files %}
{% if TargetCategory or TargetUser or TargetAssignment %}
	<span class="floatleft">
	    <a href="{% course_url management app='grades' command='manage' %}">{{course}} Grades</a>
		> <a href="{% course_url management app='grades' command='manage' arguments=TargetCategory.slug %}">{{TargetCategory.title|spacify}}</a>
		{% if TargetUser %}
			> <a href="{% course_url management app='grades' command='manage' arguments=TargetCategory.slug|add:"&&"|add:TargetAssignment.slug %}">{{TargetAssignment.title|safe}}</a>
		{% else %}
			> {{TargetAssignment.title|safe}}
	    {% endif %}
	    {% if TargetUser %}
			> {{TargetUser.first_name}}&nbsp;{{TargetUser.last_name}}
	    {% endif %}
	</span>
{% endif %}
{% autoescape off %}
<div class="form_container">
    {# Floating Save Button #}
    <form action="." method="post">{% csrf_token %}
		<button type="submit" class="button SaveButton" name="Save" value="Save" style="position: fixed; bottom: 0px; right: 0px; z-index: 5;" title="Save Changes">Save</button>
    
    
    {# Title #}
		<h1>
			<a class="AddButton button floatleft" href="{% course_url management app='grades' command='edit' arguments=TargetCategory.title %}" title="Add a new directly-entered grade to {{TargetCategory.title}}">+</a>
			Grades
		</h1>
		{% if TargetAssignment.object.allow_anonymous_grading and not TargetUser %}
			{% if not TargetAssignment.groups %}
			<input type="hidden" name="AnonymousGrading" id="AnonymousGrading" value="{{AnonymousGrading}}"></input>
			<span class="floatleft" title="Anonymous Grading removes students' names and shuffles their ordering. The default setting can be changed in the course options">
				Anonymous Grading:
				{% if AnonymousGrading %}
					<img src="{{STATIC_URL}}images/check.png" height="16">{% else %}<img src="{{STATIC_URL}}images/x.png" height="16">
				{% endif %}
				<button type="submit" class="button smallbutton" name="AnonymousGradingToggle">Toggle</button>
			</span>
			{% endif %}
		{% endif %}
		<div class="clear"></div>
		{% if Categories and Grades and Metrics %}
			<table class="grade_table grade-table-assignment left_justify" border="1" cellpadding="2">
				<tr>
					<th></th>
					{% if GroupIndex.0.0 %}
						<th></th>
					{% endif %}
					{% for category_name in CategoryIndex %}
						{% with category=Categories|getitem:category_name %}
							{% for assignment,sections in category.items %}
								{% if assignment != "info" %}
									{% with assignment_info=sections.info %}
										{% for section in sections %}
											{% if section != 'info' %}
												<th>
													{% if section.get_question %}
														<div class="popup_container">
															<span class="popup_title"><a>{{section.title}}</a></span>
															<span class="right_down_popup">{% render section.get_question.question %}</span>
														</div>
													{% else %}
														{{section.title}}<br>
													{% endif %}
													{{section.total_points}} Points
												</th>
											{% endif %}
										{% endfor %}
									{% endwith %}
								{% endif %}
							{% endfor %}
						{% endwith %}
					{% endfor %}
					<th>Total</th>
				</tr>
				{% for group,students in GroupIndex %}
					{% if group %}
						<tr><th rowspan="{{students|length|add:"1"}}">{% if AnonymousGrading and not TargetUser %}Group&nbsp;{{forloop.counter}}{% else %}{{group|spacify}}{% endif %}</th></tr>
					{% endif %}
					{% for student in students %}
						{% with grades=Grades|getitem:student %}
							{% with StudentGradingForm=GradingForms|getitem:student %}
								<tr>
									<td width=1 style="white-space: nowrap">
										{% if AnonymousGrading and not TargetUser %}
											Student&nbsp;{{forloop.counter}}
										{% else %}
											{% if grades.link %}
												<a href="{% course_url management app='grades' command='manage' arguments=grades.link %}">{{student.last_name}},&nbsp;{{student.first_name}}</a>
											{% else %}
												{{student.last_name}},&nbsp;{{student.first_name}}
											{% endif %}
											<span class="popup_container">
											<span class="popup_title"><span class="small_arrow_down"></span></span>
											<span class="right_over_popup"><button class="excuse-button" type="submit" name="excuse" value="{{student.id}}" title="Excuse this student from all sections of this assignment">Excuse</button></span>
											</span>
										{% endif %}
										
									</td>
									{% for category_name in CategoryIndex %}
										{% with assignments=grades|getitem:category_name %}
											{% for assignment,sections in assignments.items %}
												{% if assignment != 'info' %}
													{% for section in sections %}
														{% if section != 'info' %}
															{% with GradingForm=StudentGradingForm|getitem:section %}
																{% with response=GradingForm.get_response %}
																	<td>
																		<table>
																			<tr>
																				<td>
																					{% get_file_list for GradingForm.instance as Files %}
																					<div class="popup_container">
																						<div class="popup_title">
																							{{GradingForm.points}}
																							{% if GradingForm.instance.notes or Files %}*{% else %}+{% endif %}
																							{% if GradingForm.instance.excused %}<span class="excused">Excused</span>{% endif %}
																							<br>{{GradingForm.points.errors}}
																							
																							{% if response != "__None" %}
																								{{response}}
																							{% endif %}
																						</div>
																						<span class="right_down_popup">
																							<span class="popup_content">
																							<div>
																								{% if AnonymousGrading and not TargetUser %}
																									Student&nbsp;{{forloop.parentloop.parentloop.parentloop.counter}}
																								{% else %}
																									{{student.last_name}},&nbsp;{{student.first_name}}
																								{% endif %}
																								&nbsp;&nbsp;&nbsp;
																							</div>
																							<span class="floatright" title="Excuse this student from this section of the assignment">Excused?{{GradingForm.excused}}</span>
																							Notes:<br>
																							{{GradingForm.notes}}
																							{% if Files %}
																								<hr>
																								File Attachments:<br>
																								<ul>
																									{% for file in Files %}
																										<li><a href="{{file.url}}">{{file.title}} ({{file.extension}})</a>{% if file.description %} - {{file.description}}{% endif %}</li>
																									{% endfor %}
																								</ul>
																							{% endif %}
																							</span>
																						</span>
																					</div>
																				</td>
																			</tr>
																		</table>
																	</td>
																{% endwith %}
															{% endwith %}
														{% endif %}
													{% endfor %}
													<td>
														{{sections.info.percent|floatformat:"2"}}%
													</td>
												{% endif %}
											{% endfor %}
										{% endwith %}
									{% endfor %}
								</tr>
							{% endwith %}
						{% endwith %}
					{% endfor %}
				{% endfor %}
				<tr>
					<td></td>
					{% if GroupIndex.0.0 %}
						<td></td>
					{% endif %}
					{% for category in CategoryIndex %}
						{% with metrics=Metrics|getitem:category %}
							{% for assignment,sections in metrics.items %}
								{% if assignment != 'info' %}
									{% for section,info in sections.items %}
										{% if section != 'info' %}
											<td><table>
											<tr><td>Min:</td><td>{{info.min|floatformat:"2"}}</td>
											<tr><td>Max:</td><td>{{info.max|floatformat:"2"}}</td>
											<tr><td>Average:</td><td>{{info.average|floatformat:"2"}}</td>
											<tr><td>Median:</td><td>{{info.median|floatformat:"2"}}</td>
											</table></td>
										{% endif %}
									{% endfor %}
									<td>
										<table>
											<tr><td>Min:</td><td>{{sections.info.min|floatformat:"2"}}%</td></tr>
											<tr><td>Max:</td><td>{{sections.info.max|floatformat:"2"}}%</td></tr>
											<tr><td>Average:</td><td>{{sections.info.average|floatformat:"2"}}%</td></tr>
											<tr><td>Median:</td><td>{{sections.info.median|floatformat:"2"}}%</td></tr>
										</table>
									</td>
								{% endif %}
							{% endfor %}
						{% endwith %}
					{% endfor %}
				</tr>
			</table>
			<table>
				<tr>
					<td colspan="100%">
						<button class="button SaveButton" type="submit" name="Save" value="SaveContinue" title="Save and Continue Grading">Save</button>
						{% if TargetAssignment.edit_url %}
							<a class="button" href="{{TargetAssignment.edit_url}}" title="Edit this assignment and its grading sections (Save changes first!)">Edit Assignment</a>
						{% endif %}
						<a class="button" href="{% course_url management app='grades' command='groups' arguments=TargetAssignment.object.category_slug|concat:"&"|concat:TargetAssignment.object.title_slug %}" title="Assign students into groups for this assignment (Save changes first!)">Assign Groups</a>
						<a class="button" href="{% course_url management app='grades' command='manage' arguments=TargetAssignment.link|concat:"&upload" %}" title="Attach files to these grades (Save changes first!)">Attach Files</a>
						<a class="button" href="{% course_url management app='grades' command='download' %}" title="Download gradebook as .xls (Save changes first!)">Download Gradebook</a>
					</td>
				</tr>
			</table>
		{% else %}
			<p>There are no grades for this course</p>
		{% endif %}
    </form>
</div>
{% endautoescape %}
{% endblock content %}